package com.ruoyi.web.controller.api.user;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.*;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.SysLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Author: wk
 * @Date: 2022/7/27 13:53
 * @Version: 1.0
 */
@Api(tags = "前端-登录管理")
@RequiredArgsConstructor
@RestController
public class LoginApiController {
    private final SysLoginService loginService;
    private final ISysUserService userService;

    @Anonymous
    @PostMapping("/font-login")
    @ApiOperation(value = "前端-登录")
    public R<Map<String, Object>> login(@Validated @RequestBody LoginFontBody loginBody) {
        Map<String, Object> ajax = new HashMap<>();
        // 生成令牌
        String token = loginService.loginFont(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
            loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return R.ok(ajax);
    }

    /**
     * 微信登录小程序授权登录
     */
    @Anonymous
    @ApiOperation(value = "微信登录小程序授权登录")
    @RequestMapping(value = "/authorize/program/login", method = RequestMethod.POST)
    public R<LoginResponse> programLogin(@RequestParam String code, @RequestBody @Validated RegisterThirdUserRequest request){
        return R.ok(loginService.weChatAuthorizeProgramLogin(code, request));
    }

    @Anonymous
    @ApiOperation("修改后的-微信授权登录")
    @PostMapping("/weixin-login")
    public R<Map<String, Object>> xcxUpdateLogin(@RequestParam String code, @RequestBody @Validated RegisterThirdUserRequest request) {
//        if (ObjectUtil.isEmpty(code)) {
//            return R.fail("必要参数不能为空!");
//        }
//
//        // 生成令牌
//        WxUserVo wxUserInfo =json.get("userInfo", WxUserVo.class);
//        if (ObjectUtil.isEmpty(wxUserInfo)) {
//            return R.fail("用户信息不能为空！");
//        }
//
//        RegisterThirdUserRequest userInfo = wxUserInfo.getUserInfo();
//        if (ObjectUtil.isEmpty(userInfo)) {
//            return R.fail("必要信息不能为空！");
//        }
//
//        return R.ok(loginService.xcxUpdateLogin(code, userInfo));
        return R.ok();
    }

    /**
     * 微信注册绑定手机号
     */
    @Anonymous
    @ApiOperation(value = "微信注册绑定手机号")
    @RequestMapping(value = "/register/binding/phone", method = RequestMethod.POST)
    public R<LoginResponse> registerBindingPhone(@RequestBody @Validated WxBindingPhoneRequest request){
        return R.ok(loginService.registerBindingPhone(request));
    }

    @Anonymous
    @ApiOperation("微信授权登录")
    @PostMapping("/xcxLogin")
    public R<Map<String, Object>> xcxLogin(@RequestBody JSONObject json) {
        if (ObjectUtil.isEmpty(json)) {
            return R.fail("必要参数不能为空!");
        }

        String code = json.getStr("code");
        if (StringUtils.isBlank(code)) {
            return R.ok("code不能为空！");
        }
        // 生成令牌
        WxUserVo wxUserInfo =json.get("userInfo", WxUserVo.class);
        RegisterThirdUserRequest userInfo = wxUserInfo.getUserInfo();
        if (ObjectUtil.isEmpty(userInfo)) {
            return R.fail("用户信息不能为空！");
        }

        if (StringUtils.isBlank(userInfo.getUserType()) || StringUtils.isBlank(userInfo.getPhone())) {
            return R.fail("必要信息不能为空！");
        }

        return R.ok(loginService.xcxLogin(code, userInfo));
    }

    @Anonymous
    @ApiOperation("退出登录")
    @PostMapping("/log-out")
    public R<Void> logout() {
        try {
            StpUtil.logout();
        } catch (NotLoginException e) {
        }
        return R.ok("退出成功");
    }

    @ApiOperation("获取用户信息")
    @GetMapping("/get-info")
    public R<Map<String, Object>> getInfo() {
        SysUser user = userService.selectUserById(LoginHelper.getUserId());
        Map<String, Object> ajax = new HashMap<>();
        ajax.put("user", user);
        return R.ok(ajax);
    }

    @ApiOperation("前端-完善用户信息")
    @PostMapping("/edit-user")
    public R<Void> editUser(@RequestBody SysUser user) {
        userService.checkUserAllowed(user);
        userService.updateFontUser(user);
        return R.ok();
    }
}
